package com.lcj.odr.controller;

import cn.hutool.core.bean.BeanUtil;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.lcj.common.util.PageUtils;
import com.lcj.common.util.Result;
import com.lcj.odr.controller.form.*;
import com.lcj.odr.db.dao.OrderCommentDao;
import com.lcj.odr.db.pojo.OrderAppealEntity;
import com.lcj.odr.db.pojo.OrderCommentEntity;
import com.lcj.odr.service.OrderCommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/comment")
@Tag(name = "OrderCommentController", description = "订单评价模块Web接口")
public class OrderCommentController {

    @Resource
    private OrderCommentService orderCommentService;

    @Resource
    OrderCommentDao orderCommentDao;

    @PostMapping("/searchWeekGoodCommentNumber")
    @Operation(summary = "查询本周好评数")
    public Result searchWeekGoodCommentNumber(@RequestBody @Valid DriverIdForm form) {
        Long count = orderCommentService.searchWeekGoodCommentNumber(form);
        return Result.ok().put("result", count);
    }

    @PostMapping("/searchDriverAppealNumber")
    @Operation(summary = "查询司机申述数")
    public Result searchDriverAppealNumber(@RequestBody @Valid DriverIdForm form) {
        Long count = orderCommentService.searchDriverAppealNumber(form);
        return Result.ok().put("result", count);
    }



    @PostMapping("/insertComment")
    @Operation(summary = "保存订单评价")
    public Result insertComment(@RequestBody @Valid InsertCommentForm form) {
        OrderCommentEntity entity = BeanUtil.toBean(form, OrderCommentEntity.class);
        entity.setStatus((byte) 1);
        entity.setCreateTime(new Date());
        int rows = orderCommentService.insert(entity);
        return Result.ok().put("rows", rows);
    }

    @PostMapping("/searchCommentByOrderId")
    @Operation(summary = "根据订单ID查询评价")
    public Result searchCommentByOrderId(@RequestBody @Valid SearchOrderForm form) {
        Map param = BeanUtil.beanToMap(form);
        HashMap map = orderCommentService.searchCommentByOrderId(param);
        return Result.ok().put("result", map);
    }

    @PostMapping("/searchCommentByPage")
    @Operation(summary = "查询订单评价分页记录")
    public Result searchCommentByPage(@RequestBody @Valid SearchCommentByPageForm form) {
        Map param = BeanUtil.beanToMap(form);
        int page= form.getPage();
        int length = form.getLength();
        int start=(page-1)*length;
        param.put("start",start);
        PageUtils pageUtils = orderCommentService.searchCommentByPage(param);
        return Result.ok().put("result", pageUtils);
    }

    /**
     * 工作流相关:----------------------------------
     */
    @PostMapping("/acceptAppeal")
    @Operation(summary = "acceptAppeal")
    public Integer acceptAppeal(@RequestBody Map paramMap) {
        int rows = orderCommentDao.acceptAppeal(paramMap);
        return rows;
    }

    @PostMapping("/updateStatus")
    @Operation(summary = "updateStatus")
    public Integer updateStatus(@RequestBody Map paramMap) {
        int rows = orderCommentDao.updateStatus(paramMap);
        return rows;
    }

    @PostMapping("/updateRateAndRemark")
    @Operation(summary = "updateRateAndRemark")
    public Integer updateRateAndRemark(@RequestBody Map paramMap) {
        int rows = orderCommentDao.updateRateAndRemark(paramMap);
        return rows;
    }

    @PostMapping("/searchCommentIdAndInstanceId")
    @Operation(summary = "searchCommentIdAndInstanceId")
    public HashMap searchCommentIdAndInstanceId(@RequestBody Map paramMap) {
        OrderCommentForm orderCommentForm = BeanUtil.toBean(paramMap, OrderCommentForm.class);
        HashMap commentIdAndInstanceId = orderCommentDao.searchCommentIdAndInstanceId(orderCommentForm);
        return commentIdAndInstanceId;
    }

    @PostMapping("/startWorkflow")
    @Operation(summary = "startWorkflow")
    @Transactional
    @LcnTransaction
    public int startWorkflow(@RequestBody Map paramMap) {
        int rows = orderCommentDao.startWorkflow(paramMap);
        if(rows > 0) {
            OrderAppealEntity orderAppealEntity = BeanUtil.toBean(paramMap, OrderAppealEntity.class);
            rows = orderCommentDao.insertOrderCommentAppeal(orderAppealEntity);
        }
        return rows;
    }
    /**---------------------------------------------------**/
}